package com.nkzy.mapper;

import com.nkzy.pojo.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper // Ioc
public interface AddressMapper {

    @Insert("INSERT INTO address (user_id, street, city, state, zip_code, create_time, update_time) " +
            "VALUES (#{userId}, #{street}, #{city}, #{state}, #{zipCode}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertAddress(Address address);

    @Delete("DELETE FROM address WHERE id = #{id}")
    void deleteAddressById(Long id);

    @Update("UPDATE address SET user_id = #{userId}, street = #{street}, city = #{city}, state = #{state}, " +
            "zip_code = #{zipCode}, update_time = #{updateTime} WHERE id = #{id}")
    void updateAddress(Address address);

    @Select("SELECT * FROM address WHERE id = #{id}")
    Address selectAddressById(Long id);

    @Select("SELECT * FROM address")
    List<Address> selectAllAddresses();

    @Select("<script>" +
            "SELECT * FROM address WHERE id IN " +
            "<foreach item='id' index='index' collection='list' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Address> selectAddressesByIds(List<Long> ids);
}